﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace EdexApiClient
{
    public partial class EdexApiContext
    {
        /// <summary>
        /// Получить перечень компаний
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable<WH_COMPANIES> GetCompanies()
        {
            return this.GetCompanies(String.Empty);
        }

        /// <summary>
        /// Получить перечень компаний
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable<WH_COMPANIES> GetCompanies(string search)
        {
            var uri = String.Format("Companies/?api_key={0}", this.session.APIKEY);

            if (!String.IsNullOrEmpty(search))
                uri += "&$filter=substringof('" + search + "', COMPANY_NAME) eq true&$top=100";

            uri += "&$orderby=COMPANY_NAME";

            return this.GetData<List<WH_COMPANIES>>(uri);
        }

        public WH_COMPANIES GetCompanyDetails(Guid id)
        {
            var uri = String.Format("Companies/{0}/?api_key={1}", id.ToString(), this.session.APIKEY);
            return this.GetData<WH_COMPANIES>(uri);
        }

        /// <summary>
        /// Получить перечень компаний, к которым предоставлен доступ
        /// </summary>
        /// <returns></returns>
        public IEnumerable<SEC_USER_COMPANIES> GetAllowedCompanies()
        {
            return this.GetData<List<SEC_USER_COMPANIES>>(String.Format("SEC_USER_COMPANIES/?api_key={0}", this.session.APIKEY));
        }

        public void AddCompany(WH_COMPANIES data)
        {
            var uri = String.Format("Companies/?api_key={0}", this.session.APIKEY);
            this.Post<WH_COMPANIES>(uri, data);
        }

        public void UpdateCompany(WH_COMPANIES data)
        {
            var uri = String.Format("Companies/?api_key={0}", this.session.APIKEY);
            this.Put<WH_COMPANIES>(uri, data);
        }

        public void DeleteCompany(string id)
        {
            var uri = String.Format("Companies/{0}/?api_key={1}", id, this.session.APIKEY);
            this.Delete(uri);
        }

        public COMPANY_MAP GetCompanyMap(Guid SUPPLIER_GUID)
        {
            var uri = String.Format("COMPANIES_MAPPING/?SUPPLIER_GUID={0}&api_key={1}", SUPPLIER_GUID, this.session.APIKEY);
            return this.GetData<COMPANY_MAP>(uri);
        }

        public void SaveCompanyMap(Guid COMPANY_GUID, Guid SUPPLIER_GUID)
        {
            this.Post<COMPANY_MAP>(String.Format("COMPANIES_MAPPING/?api_key={0}", this.session.APIKEY),
                new COMPANY_MAP
                {
                    LEFT_COMPANY_MAP = new WH_COMPANIES { GUID_RECORD = COMPANY_GUID },
                    RIGHT_COMPANY_MAP = new WH_COMPANIES { GUID_RECORD = SUPPLIER_GUID }
                });
        }
    }
}